<?php
    /**
    * Email : mr.linhtx88@gmail.com   
    * Phone : +84919.021.508
    * 
    * History       :
    *   Date        : 16-Apr-2012
    *   Modified by : mr.linhtx88@gmail.com        
    *   Purpose     : create class utility
    */

    class utility{     
        /*
        * link 
        */
        public $link = "http://stco.vn/diendan/showthread.php?";   

        /*
        *   get link of theard
        *  
        *   @input  :   $threadid
        *   &output :   link url
        * 
        *   example :   $threadid=5  link url = http://stco.vn/diendan/showthread.php?5
        */
        public function getLink($threadid){
            $link1 = "http://stco.vn/diendan/showthread.php?".$threadid;
            return $link1;              
        }   

        /*
        *   get link of youtube
        *  
        *   @input  :   $linkVideo
        *   &output :   link url
        * 
        *   example :   $linkVideo=[url]www.http://....[url]  link url = www.http://....
        */
        public function getLinkVideo($linkVideo){
            $link1 = null;

            $link1 = str_replace('[/url]','',$linkVideo);
            $link1 = str_replace('[url]','',$link1);  
            return $link1;              
        }        

        /*
        *   get linkImage from link of youtube
        *  
        *   @input  :   $linkVideo
        *   &output :   linkImage
        * 
        *   example :   $linkVideo = http://www.youtube.com/watch?v=l3B3oT8vd2M  
        *               linkImage = http://img.youtube.com/vi/l3B3oT8vd2M/hqdefault.jpg
        */
        public function getImageFromLinkVideo($linkVideo){
            $link1 = null;

            $link1 = str_replace('[/url]','',$linkVideo);
            $link1 = str_replace('[url]','',$link1);  

            $link1 = str_replace('www','img',$link1);    
            $link1 = str_replace('watch?v=','vi/',$link1);      
            $link1 = $link1.'/hqdefault.jpg';

            return $link1;              
        }
        /* videoLinkPaser dung de tack link video ra khoi doan text */
        public function videoLinkPaser($text){
            // $link : l? link ch?nh x?c c?a youtube video d?ng : http://youtube.com/watch?=12344567
            // $text : l? text select ra tu pagetext 
            $link = null;
            $link = str_replace('[/url]','',$text);
            $link = str_replace('[url]','',$link);
            return $link;
        }

        //v? do?n reguler expression kh?ng x?a du?c tag url --> x?a 2 c?i tag n?y tru?c
        public function removeTagUrl($text){
            // $link : l? link ch?nh x?c c?a youtube video d?ng : http://youtube.com/watch?=12344567
            // $text : l? text select ra tu pagetext 
            $clearedUrl = null;
            $clearedUrl = str_replace('[/url]','',$text);
            $clearedUrl = str_replace('[url]','',$clearedUrl);
            return $clearedUrl;
        }
        public function filterUrlFromText($text){
            // The Regular Expression filter
            $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

            // Check if there is a url in the text               
            if(preg_match($reg_exUrl, $text, $url)) {
                // make the urls hyper links
                // echo preg_replace($reg_exUrl, "Ket qua : ".$url[0], $linkVideo);
                preg_replace($reg_exUrl,$text,$url);
                return $url; 
            } else {

                // if no urls in the text just return the text
                echo "L?i l?y link video trong di?n d?n S? Ti C?";   
                return ;
            }
            // c?u tr?c url return:  
            //url[0] : full video link gi?ng nhu khi m?i paste v?o di?n d?n
            //url[1] :'http'
            //url[2]: to?n b? link sau ch? yuotobe.com/

        }
        
        // check input - SQL injection
        public function CheckInput($input){    
            $input = strtoupper($input);
            $input = str_replace(' ','', $input);  
            
            $input = str_replace('--', '', $input);   
            $input = str_replace("'", '', $input);    
            $input = str_replace("or1", '', $input); 
            $input = str_replace("1=1", '', $input);  
            $input = str_replace("having", '', $input);  
            $input = str_replace("union", '', $input);    
            $input = str_replace("all", '', $input);       
            $input = str_replace("drop", '', $input); 
            $input = str_replace("sa", '', $input);  
            $input = str_replace("admin", '', $input); 
            
            return $input;               
        }
    }
?>
